package com.doit.day03

import scala.collection.immutable
import scala.io.Source

object _06_线段重叠案例 {
  def main(args: Array[String]): Unit = {
    /*
    1,4
    2,5
    4,6
    2,4
    3,6
    4,6
    1,5
     */

    //1.读取文件，将每一行的值放在集合中
    val list: List[String] = Source.fromFile("data/abc.txt")
      .getLines()
      .toList


    //2.for循环 Array(Array(1-2,2-3,3-4),Array(2-3,3-4,4-5).....)
    val res: immutable.Seq[immutable.Seq[String]] = for (elem <- list) yield{
      val arr: Array[String] = elem.split(",")
      val start: Int = arr(0).toInt
      val end: Int = arr(1).toInt

      val seq: immutable.Seq[Int] = start.until(end)
      val lines = for (elem1 <- seq) yield {
        //1-2 2-3 3-4
        elem1+"-"+(elem1+1)
      }
      lines
    }

    val words: List[String] = list.flatMap(line => {
      val arr: Array[String] = line.split(",")
      val start: Int = arr(0).toInt
      val end: Int = arr(1).toInt
      val seq: immutable.Seq[Int] = start.until(end)
      val lines = for (elem1 <- seq) yield {
        //1-2 2-3 3-4
        elem1 + "-" + (elem1 + 1)
      }
      lines
    })

    //3.wordCount
    val res1 :Map[String,List[String]]= words.groupBy(word=>word)
//    res1.foreach(println)
    //求value的个数
    res1.map(tp=>(tp._1,tp._2.size))
      .foreach(println)




  }

}
